package com.iflytek.jobs.admin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.AjaxResult;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("/jobs/python/server")
public class PythonController {
    
    @PostMapping("/pdf2txt")
    public AjaxResult pdf2txt(HttpServletRequest request, @RequestBody String pdfUrl) {
        // 获取当前服务地址
        String serverUrl = request.getServerName();
        int serverPort = request.getServerPort();
        String contextPath = request.getContextPath();
        String protocol = request.getScheme();
        String baseUrl = protocol + "://" + serverUrl + ":" + serverPort + contextPath;
        pdfUrl = baseUrl + pdfUrl;
        // 请求体对象JSON对象
        JSONObject body = new JSONObject();
        body.set("url", pdfUrl);
        String result = HttpRequest.post("http://127.0.0.1:12002/pdf2txt")
        .body(body.toString())
        .execute()
        .body();
        //result转化为json
        JSONObject resultJson = JSONUtil.parseObj(result);
        return AjaxResult.success(resultJson.get("data").toString());
    }

}
